package com.lxmes.sales.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.sales.domain.NcClientFiles;
import com.lxmes.sales.domain.NcContract2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.sales.domain.NcContract;
import com.lxmes.sales.service.INcContractService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 合同管理表Controller
 *
 * @author ruoyi
 * @date 2022-11-29
 */
@RestController
@RequestMapping("/sales/contract")
public class NcContractController extends BaseController
{
    @Autowired
    private INcContractService ncContractService;

    /**
     * 查询合同管理表列表
     */
    @PreAuthorize("@ss.hasPermi('sales:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcContract ncContract)
    {
        startPage();
        List<NcContract> list = ncContractService.selectNcContractList(ncContract);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('sales:contract:list')")
    @GetMapping("/list2")
    public TableDataInfo list2(NcContract ncContract)
    {
        startPage();
        List<NcContract> list = ncContractService.selectNcContractListContainItem(ncContract);
        return getDataTable(list);
    }




    /**
     * 导出合同管理表列表
     */
    @PreAuthorize("@ss.hasPermi('sales:contract:export')")
    @Log(title = "合同管理表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcContract ncContract)
    {
        List<NcContract> list = ncContractService.selectNcContractList(ncContract);

        ExcelUtil<NcContract> util = new ExcelUtil<NcContract>(NcContract.class);
        util.exportExcel(response, list, "合同管理表数据");
    }

    /**
     * 获取合同管理表详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:contract:query')")
    @GetMapping(value = "/{uuid}")
    public AjaxResult getInfo(@PathVariable("uuid") String uuid)
    {
        return AjaxResult.success(ncContractService.selectNcContractByUuid(uuid));
    }
    /**
     * 获取合同管理表详细信息
     */
    @PreAuthorize("@ss.hasPermi('sales:contract:query')")
    @GetMapping(value = "/getContractDetail/{uuid}")
    public AjaxResult getContractDetail(@PathVariable("uuid") String uuid)
    {
        return AjaxResult.success(ncContractService.selecgetContractDetailByUuid(uuid));
    }


    /**
     * 生成订单
     */
    @PreAuthorize("@ss.hasPermi('sales:contract:edit')")
    @GetMapping("/scOrder/{uuid}")
    public AjaxResult scOrder(@PathVariable("uuid") String uuid)
    {
        NcContract ncContract = ncContractService.selectNcContractByUuid(uuid);
        if(ncContract.getIsGenOrder().equals("1")){
            return AjaxResult.error("该合同已生成了销售订单,不能重复生成。");
        }

        int scht = ncContractService.scOrder(ncContract);
        if(scht==1){
            return AjaxResult.success("生成成功！");
        }else{
            return AjaxResult.error("生成失败！");
        }
    }

    /**
     * 新增合同管理表
     */
    @PreAuthorize("@ss.hasPermi('sales:contract:add')")
    @Log(title = "合同管理表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcContract ncContract)
    {
        return toAjax(ncContractService.insertNcContract(ncContract));
    }

    /**
     * 修改合同管理表
     */
    @PreAuthorize("@ss.hasPermi('sales:contract:edit')")
    @Log(title = "合同管理表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcContract ncContract)
    {
        return toAjax(ncContractService.updateNcContract(ncContract));
    }


    @PreAuthorize("@ss.hasPermi('sales:contract:edit')")
    @GetMapping("/applQd")
    public AjaxResult applQd(NcContract ncContract)
    {
        return toAjax(ncContractService.updateNcContract(ncContract));
    }


    /**
     * 删除合同管理表
     */
    @PreAuthorize("@ss.hasPermi('sales:contract:remove')")
    @Log(title = "合同管理表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{uuids}")
    public AjaxResult remove(@PathVariable String[] uuids)
    {
        return toAjax(ncContractService.deleteNcContractByUuids(uuids));
    }


    @Log(title = "合同管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, NcContract2 ncContract2) throws Exception
    {
        ExcelUtil<JcIteminfo> util = new ExcelUtil<>(JcIteminfo.class);
        List<JcIteminfo> infoList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        NcContract ncContract = new NcContract();
        BeanUtils.copyProperties(ncContract2,ncContract);
        String message = ncContractService.imporIteminfo(infoList, updateSupport, operName,ncContract);

        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcContract> util = new ExcelUtil<>(NcContract.class);
        util.importTemplateExcel(response,"合同数据");
    }
}
